﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using elmah2.core.config;
using elmah2.core.interfaces;
using elmah2.core.encoders;

namespace elmah2.Viewer
{
  public partial class ViewError : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string logger = GetQueryData("logger");
      string id = GetQueryData("error");

      if (string.IsNullOrEmpty(logger) || string.IsNullOrEmpty(id))
      {
        //todo show error
        Response.Clear();
        Response.End();
      }
      else
      {
        IErrorLogger elogger = Logger.GetLogger(logger);
        elmah2.core.data.Error er = elogger.GetError(id);

        if (er != null)
        {
          content.Text = HtmlEncoder.EncodeString(er);
          errorMsg.Text = er.Message;          
        }
        else
        {
          //todo show error
          Response.Clear();
          Response.Write("Error not found");
          Response.End();
        }
      }
    }

    private string GetQueryData(string parm)
    {
      string value = "";
      foreach (string s in Request.QueryString.AllKeys)
      {
        if (s.Equals(parm, StringComparison.InvariantCultureIgnoreCase))
        {
          value = HttpUtility.UrlDecode(Request.QueryString[parm]);
          break;
        }
      }
      return value;
    }
  }
}
